import time


def to_RGB(color):
    """topLed:
       颜色color字符串
       rgb转换
       红：red (255,0,0)
       绿：green (0,255,0)
       蓝：blue (0,0,255)

       黄：yellow (255,255,0)
       黑：black (0,0,0)
       白：white (255,225,255)
       紫：purple (255,0,255)
       """
    """
    latticeLed:
    只有r（red）、b（blue）、p（purple）
    """
    rgb = []
    if color == "red":
        rgb = [255, 0, 0]
    elif color == "green":
        rgb = [0, 255, 0]
    elif color == "blue":
        rgb = [0, 0, 255]
    elif color == "black":
        rgb = [0, 0, 0]
    elif color == "white":
        rgb = [255, 255, 255]
    elif color == "yellow":
        rgb = [255, 255, 0]
    elif color == "purple":
        rgb = [255, 0, 255]
    return rgb


def to_Direction(direction):
    """方向direction字符串
       上：up u
       下：down d
       左：left l
       右：right r
       """
    d = None
    if direction == "up":
        d = "u"
    elif direction == "down":
        d = "d"
    elif direction == "right":
        d = "r"
    elif direction == "left":
        d = "l"
    return d


class Led:
    connect = None

    """频率f"""

    def __init__(self, connect):
        self.connect = connect

    def topLed_Single_withoutBr(self, color):
        """   点亮顶部led为指定颜色
        color为颜色字符串"""
        rgb = to_RGB(color)
        self.connect.send_ESP32("led {r} {g} {b}".format(r=rgb[0], g=rgb[1], b=rgb[2]))
        return self.connect.rec_result()

    def topLed_Single_withBr(self, f, color):
        """顶部 led按照呼吸最大亮度 颜色和呼吸频率t显示呼吸效果。
        从最暗变化到最亮再到最暗为一次呼吸。
        color为颜色字符串
        f: 0.1~2.5Hz
        """
        rgb = to_RGB(color)
        self.connect.send_ESP32("led br {f} {r} {g} {b}".format(f=f, r=rgb[0], g=rgb[1], b=rgb[2]))
        return self.connect.rec_result()

    def topLed_Blink(self, f, color1, color2):
        """顶部led以颜色1和颜色2按照闪烁频率t交替闪烁
        color1、color2为颜色字符串
        f: 0.1~10Hz"""
        rgb1 = to_RGB(color1)
        rgb2 = to_RGB(color2)
        self.connect.send_ESP32(
            "led bl {f} {r1} {g1} {b1} {r2} {g2} {b2}".format(f=f, r1=rgb1[0], g1=rgb1[1], b1=rgb1[2], r2=rgb2[0],
                                                              g2=rgb2[1], b2=rgb2[2]))
        return self.connect.rec_result()

    def latticeLed_StringImage_Stabilize(self, s):
        """点亮点阵屏为指定图案
        s：代表仅由‘r’ ‘b’ ‘p’ ‘0’组成的字符串，‘r’ ‘b’ ‘p’ ‘0’分别代表红、蓝、紫、灭，字符串长度最大为 64。"""
        self.connect.send_ESP32("mled g {s}".format(s=s))
        return self.connect.rec_result()

    def latticeLed_String(self, direction, color, f, s):
        """  点阵屏显示字符串移动效果
        direction为方向字符串
        r / b / p代表字符串显示颜色
        f: 0.1~2.5Hz, 代表移动一帧图像频率
        s代表需要显示的字符串，字符串最大长度需要小于70
        """
        d = to_Direction(direction)
        self.connect.send_ESP32(
            "mled {direction} {color} {f} {s}".format(direction=d, color=color, f=f, s=s))
        return self.connect.rec_result()

    def latticeLed_StringImage_Move(self, direction, f, s):
        """点阵屏显示图像移动
        direction为方向字符串
        r/b/p 代表字符串显示颜色(NONONONONO!!!!!!!!!!!!!!!!!)
        f: 0.1~2.5Hz, 代表一帧图像移动频率
        s：代表仅由‘r’ ‘b’ ‘p’ ‘0’组成的字符串，‘r’ ‘b’ ‘p’ ‘0’分别代表红、蓝、紫、灭，字符串长度最大为 64。
        """
        d = to_Direction(direction)
        self.connect.send_ESP32("mled {direction} g {f} {s}".format(direction=d, f=f, s=s))
        return self.connect.rec_result()

    def latticeLed_ASCIIorHeart(self, color, s):
        """
        显示静态 ASCII 字符或预设图案
        r/b/p 代表字符串显示颜色
        s 仅可取单个字符char
        """
        self.connect.send_ESP32("mled s {color} {s}".format(color=color, s=s))
        return self.connect.rec_result()

    def latticeLed_PowerOn(self, s):
        """设置点阵开机动画，设置后每次开机后会显示用户设置的图案
        s：代表仅由‘r’ ‘b’ ‘p’ ‘0’组成的字符串，‘r’ ‘b’ ‘p’ ‘0’分别代表红、蓝、紫、灭，字符串长度最大为 64。"""
        self.connect.send_ESP32("mled sg {s}".format(s=s))
        return self.connect.rec_result()

    def latticeLed_Clean(self):
        """    清除设置的开机动画"""
        self.connect.send_ESP32("mled sc")
        return self.connect.rec_result()

    def latticeLed_Clean(self, n):
        """设置点阵亮度
        n: 0~255
        """
        self.connect.send_ESP32("mled sl {n}".format(n=n))
        return self.connect.rec_result()

    def Led_arrogant(self):
        s = "00000000" + "00000000" + "0rrr0rrr" + "00rr00rr" + "00000000" + "0000rrr0" + "000r0000" + "00000000"
        self.connect.send_ESP32("mled g {s}".format(s=s))
        return self.connect.rec_result()

    def topLedspe(self, f, color1, color2, cot):
        """定量led指定颜色的呼吸灯
        f: 0.1~2.5Hz
        color为颜色字符串
        cot亮闪次数
        """
        stop_time = time.clock() + 1 / f * cot
        rgb1 = to_RGB(color1)
        rgb2 = to_RGB(color2)
        self.connect.send_ESP32("led br {f} {r} {g} {b}".format(f=f, r=rgb1[0], g=rgb1[1], b=rgb1[2]))
        return self.connect.rec_result()

    def led_mid(self, mid):
        """
        点阵屏单字符显示
        mid 为单个字符
        指示灯以频率1Hz闪烁红灯三次后关闭
        """
        self.latticeLed_ASCIIorHeart('b', mid)
        self.topLedspe(1, "blue", "blue", 3)
